//
//  SLPhotoBrowserManager.m
//  SLPhotoBrowser
//
//  Created by S_LINE on 2020/6/21.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLPhotoBrowserManager.h"

#import "SLPhotoBrowserController.h"

#import <SLKit/SLKit.h>

@interface SLPhotoBrowserManager ()

@property (nonatomic, strong) SLPhotoBrowserController *currentPhotoBrowserVC;

@end

@implementation SLPhotoBrowserManager

+ (instancetype)defaultManager {
    static dispatch_once_t onceToken;
    static SLPhotoBrowserManager *defaultManager;
    dispatch_once(&onceToken, ^{
        defaultManager = [[self alloc] init];
    });
    return defaultManager;
}

- (void)photoBrowserWithUrls:(NSArray *)imageUrls currentIndex:(NSUInteger)currentIndex {
    if (![SLSafe isEffectiveArray:imageUrls]) {
        return;
    }
    
    if (self.currentPhotoBrowserVC) {
        [self.currentPhotoBrowserVC.view removeFromSuperview];
        self.currentPhotoBrowserVC = nil;
    }
    NSMutableArray <SLPhotoModel *> *photoModels = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageUrls.count; i++) {
        SLPhotoModel *photoModel = [[SLPhotoModel alloc] init];
        photoModel.imageUrl = [imageUrls sl_objectAtIndex:i];
        [photoModels sl_addObject:photoModel];
    }
    
    [self photoBrowserWithImages:photoModels currentIndex:currentIndex];
}

- (void)photoBrowserWithImages:(NSArray<SLPhotoModel *> *)images currentIndex:(NSUInteger)currentIndex {
    [self photoBrowserWithImages:images currentIndex:currentIndex removeable:NO removeCallback:nil];
}

/// 显示图片
/// @param images 图片模型数组
/// @param currentIndex 当前默认显示的图片的index
/// @param isRemoveable 是否可以删除
/// @param removeCallback 删除了某张照片后的回调
- (void)photoBrowserWithImages:(NSArray<SLPhotoModel *> *)images
                  currentIndex:(NSUInteger)currentIndex
                    removeable:(BOOL)isRemoveable
                removeCallback:(void(^ _Nullable)(NSInteger index))removeCallback {
    SLPhotoBrowserController *photoBrowserVC = [[SLPhotoBrowserController alloc] initWithBrowserType:SLPhotoBrowserType_URL];
    photoBrowserVC.removeable = isRemoveable;
    self.currentPhotoBrowserVC = photoBrowserVC;
    photoBrowserVC.images = [images mutableCopy];
    photoBrowserVC.currentIndex = currentIndex;
    __weak typeof(self) weakSelf = self;
    photoBrowserVC.backActionBlock = ^{
        [weakSelf hidePhotoBrowserController];
    };
    photoBrowserVC.photoRemovedCallBack = ^(NSInteger index) {
        if (removeCallback) {
            removeCallback(index);
        }
    };
    
    [self showPhotoBrowserController];
}

- (void)showPhotoBrowserController {
    if (self.currentPhotoBrowserVC.view.superview) {
        return;
    }
    
    self.currentPhotoBrowserVC.view.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:self.currentPhotoBrowserVC.view];
    [UIView animateWithDuration:0.33 animations:^{
        self.currentPhotoBrowserVC.view.alpha = 1;
    }];
}

- (void)hidePhotoBrowserController {
    if (!self.currentPhotoBrowserVC.view.superview) {
        return;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.currentPhotoBrowserVC.view];
    [UIView animateWithDuration:0.33 animations:^{
        self.currentPhotoBrowserVC.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.currentPhotoBrowserVC.view removeFromSuperview];
        self.currentPhotoBrowserVC = nil;
    }];
}

@end
